﻿namespace EFS.RC.CommunicatorContext.Operators
{
	public class Controls : EFS.RC.CommunicatorContext.Common.Pages.Controls.Control
	{
		// paused
		EFS.RC.CommunicatorContext.Recorder.Paused recorderPaused = null;

		/// Enum ButtonType
		public enum ButtonType
		{
			Monitor,
			Whisper,
			BargeIn,
			Hold,
			Resume,
			RecorderPause,
			RecorderResume
		}

		/// Class ButtonsEventArgs
		public class ButtonsEventArgs : System.EventArgs
		{
			public ButtonsEventArgs(ButtonType buttonType)
			{
				this.ButtonType = buttonType;
			}

			/// Property ButtonType: Get,Set
			public ButtonType ButtonType { get; private set; }
		}

		#region EventHandlers
		/// Event used when a message has been received
		public event System.EventHandler<EFS.RC.CommunicatorContext.Operators.Controls.ButtonsEventArgs> Button_Clicked;
		#endregion

		public Controls()
			: base()
		{
			// set loaded event
			this.Loaded += new System.Windows.RoutedEventHandler(Controls_Loaded);

			// conversation guid
			ConversationGuid = System.Guid.Empty;
		}

		#region Properties
		/// Property ButtonHold: Get
		protected virtual System.Windows.Controls.Button ButtonHold
		{
			get
			{
				throw new System.NotImplementedException("ButtonHold");
			}
		}

		/// Property ButtonResume: Get
		protected virtual System.Windows.Controls.Button ButtonResume
		{
			get
			{
				throw new System.NotImplementedException("ButtonResume");
			}
		}

		/// Property ConversationGuid: Get, Set
		public System.Guid ConversationGuid { get; private set; }
		#endregion

		#region Utilities
		/// Update buttons
		public virtual void UpdateInfo(EFS.RC.CC.Messages.Communicator.Lync.Conversation.Info conversationInfo)
		{
			// check
			EFS.Common.Verify.Reference(conversationInfo, "conversationInfo");

			// switch
			ButtonResume.IsEnabled = conversationInfo.IsOnHold;
			ButtonHold.IsEnabled = !ButtonResume.IsEnabled;

			// checo pause recorder count down
			if (null != conversationInfo.RecorderPauseLength)
			{
				// check recorder
				if (null == recorderPaused)
				{
					recorderPaused = new Recorder.Paused();
					recorderPaused.Closed += new System.EventHandler(recorderPaused_Closed);
					recorderPaused.Show();
				}

				// set count down
				recorderPaused.SetPauseLength(conversationInfo.RecorderPauseLength.Value);
			}
			else
			{
				// close recorder
				if (null != recorderPaused)
				{
					recorderPaused.Close();
				}
			}

			// keep guid
			ConversationGuid = conversationInfo.Guid;
		}

		#endregion

		#region Events
		/// OnLoaded
		protected virtual void OnLoaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				// set events
				ButtonHold.Click += new System.Windows.RoutedEventHandler(ButtonHold_Click);
				ButtonResume.Click += new System.Windows.RoutedEventHandler(ButtonResume_Click);
			}
		}

		/// Called when the controls has loaded
		private void Controls_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			OnLoaded(sender, e);
		}

		/// HandleButtonClicked
		protected virtual void HandleButtonClicked(object sender, ButtonsEventArgs e)
		{
			if (null != this.Button_Clicked)
			{
				this.Button_Clicked(sender, e);
			}
		}

		/// Resume clicked
		private void ButtonResume_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			HandleButtonClicked(sender, new ButtonsEventArgs(ButtonType.Resume));
		}

		/// Hold clicked
		private void ButtonHold_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			HandleButtonClicked(sender, new ButtonsEventArgs(ButtonType.Hold));
		}

		void recorderPaused_Closed(object sender, System.EventArgs e)
		{
			// remove recorder
			recorderPaused = null;
			
			// click
			HandleButtonClicked(sender, new ButtonsEventArgs(ButtonType.RecorderResume));
		}
		#endregion
	}

}